//
//  AppDelegate.m
//  本地生活圈
//
//  Created by admin on 15/8/9.
//  Copyright (c) 2015年 懒人. All rights reserved.
// A -add  M--修改 ？--布吉岛
// 登录作业 home collection 作业

#import "AppDelegate.h"
#import "LRGuideViewController.h"
#import "LRTabBarViewController.h"
#import "LRHomeViewController.h"
#import "LRNewsViewController.h"
#import "LRCircleViewController.h"
#import "LRToolsViewController.h"
#import "UMSocial.h"
#import "LRUserInfo.h"
#import <WeiboSDK.h>

@interface AppDelegate ()<WeiboSDKDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    // 启动图片导致的frame

    [self.window makeKeyAndVisible];
    
    // 注册一个通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(rootViewControllerChange) name:kLogin object:nil];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    // 判断是否第一次登陆
    if (![user boolForKey:kLogin])
    {
        LRGuideViewController *guideVc = [[LRGuideViewController alloc]init];
        self.window.rootViewController = guideVc;
    }
    else
         self.window.rootViewController = [self rootViewControllerChange];
    
    // 是否记住密码
    if ([user boolForKey:kRememberPwd])
    {
        // 加载个人信息
        NSDictionary *dict = [LRFunction getUserInfo];
        [LRUserInfo pareseUserInfo:dict];
    }
    
    // 友盟分享
    [UMSocialData setAppKey:AppUmKey];
    
    // 微博注册
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:AppWeiBoKey];
    return YES;
}

#pragma mark - 微博  重写open方法
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}

- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
    LRLog(@"%@",response); //liguohui_1@163.com
    switch (response.statusCode)
    {
        case WeiboSDKResponseStatusCodeSuccess:
            [MBProgressHUD showSuccess:@"授权成功"];
            break;
        default:
            [MBProgressHUD showError:@"授权失败"];
            break;
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (UIViewController *)rootViewControllerChange
{
    // tabbar
    LRTabBarViewController *tabBar = [[LRTabBarViewController alloc]init];
    // 从sb获得控制器
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    LRHomeViewController *home = [sb instantiateViewControllerWithIdentifier:@"LRHomeViewController"];
    UINavigationController *homeNav = [[UINavigationController alloc]initWithRootViewController:home];
    
    LRNewsViewController *news = [sb instantiateViewControllerWithIdentifier:@"LRNewsViewController"];
    UINavigationController *newsNav = [[UINavigationController alloc]initWithRootViewController:news];
    
    LRCircleViewController *circle = [sb instantiateViewControllerWithIdentifier:@"LRCircleViewController"];
    UINavigationController *circleNav = [[UINavigationController alloc]initWithRootViewController:circle];
    
    LRToolsViewController *tool = [sb instantiateViewControllerWithIdentifier:@"LRToolsViewController"];
    UINavigationController *toolNav = [[UINavigationController alloc]initWithRootViewController:tool];
    
    tabBar.viewControllers = @[homeNav,newsNav,circleNav,toolNav];
    self.window.rootViewController = tabBar;
    return tabBar;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
